import { RootState, RootThunkAction } from '@/types/store'
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

// useInitState(xxx)
export function useInitState(fn: () => RootThunkAction) {
  const dispatch = useDispatch()

  useEffect(() => {
    // 封装一个函数返回异步action
    dispatch(fn())
  }, [dispatch, fn])

  const state = useSelector<RootState, RootState>((state) => state)

  return state
}
